package com.kun.service_member.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kun.service_member.entity.UcenterMember;
import com.kun.service_member.entity.vo.RegisterVo;
import com.kun.service_member.mapper.UcenterMemberMapper;
import com.kun.service_member.service.UcenterMemberService;
import com.kun.servicebase.exceptionhandler.KunException;
import com.kun.utils.JwtUtils;
import com.kun.utils.MD5;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-10-25
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Override
    public String login(UcenterMember member) {
        String mobile = member.getMobile();
        String password = member.getPassword();

        //手机号和密码非空判断
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new KunException(20001,"用户名密码不能为空");
        }

        //判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);
        //判断查询对象是否为空
        if(mobileMember == null) {//没有这个手机号
            throw new KunException(20001,"手机号为注册");
        }

        //判断密码
        //因为存储到数据库密码肯定加密的
        //把输入的密码进行加密，再和数据库密码进行比较
        //加密方式 MD5
        if(!MD5.encrypt(password).equals(mobileMember.getPassword())) {
            throw new KunException(20001,"密码错误");
        }

        //判断用户是否禁用
        if(mobileMember.getIsDisabled()) {
            throw new KunException(20001,"用户已被禁用");
        }
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
        return jwtToken;
    }

    @Override
    public void register(RegisterVo registerVo) {
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();
        if (StringUtils.isEmpty(code)||StringUtils.isEmpty(mobile)||StringUtils.isEmpty(code)||StringUtils.isEmpty(password)){
            throw new KunException(20001,"不能为空");

        }
        System.out.println(code);
        if (!code.equals("123456")){
            throw new KunException(20001,"验证码错误");
        }
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if (count>0){
            throw new KunException(20001,"账号已注册");
        }
        UcenterMember member = new UcenterMember();
        member.setPassword(MD5.encrypt(password));
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setIsDisabled(false);
        member.setAvatar("https://zxjydmk.oss-cn-beijing.aliyuncs.com/2021/10/23/892cbdacbbd74e308657c2049cfc2325file.png");
        baseMapper.insert(member);
    }

    @Override
    public UcenterMember getMemberByOpenId(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;

    }

    @Override
    public int countRegisterDay(String day) {
        return baseMapper.countRegisterDay(day);
    }
}
